"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GmUserModel = void 0;
const ErrorCode_1 = require("../../src/config/ErrorCode");
const Md5Helper_1 = require("../../src/engine/utils/Md5Helper");
const ResponseModel_1 = require("../../src/model/ResponseModel");
class GmUserModel {
    constructor(userId = 0, userName = "", pass = "") {
        this.userId = userId;
        this.userName = userName;
        this.pass = pass;
    }
    static checkLogin(userName, pass) {
        return __awaiter(this, void 0, void 0, function* () {
            let respModel = new ResponseModel_1.ResponseModel();
            respModel.code = ErrorCode_1.ErrorCode.Code.Suc;
            if (this.userMap.has(userName)) {
                let model = this.userMap.get(userName);
                if (model.pass == pass) {
                    let token = Md5Helper_1.Md5Helper.createToken(userName);
                    model.token = token;
                    respModel.data = model;
                    this.removeSession(model.userId);
                    this.insertSession(token, model.userId);
                }
                else {
                    respModel.code = ErrorCode_1.ErrorCode.Code.LoginError_NoPass;
                    respModel.msg = "密码不对";
                }
            }
            else {
                respModel.code = ErrorCode_1.ErrorCode.Code.LoginError_NoPass;
                respModel.msg = "用户不存在";
            }
            return respModel;
        });
    }
    static removeSession(userId) {
        this.session.forEach((value, key) => {
            if (value == userId) {
                this.session.delete(key);
                return;
            }
        });
    }
    static insertSession(token, userId) {
        this.session.set(token, userId);
    }
    static hasSession(token) {
        return this.session.has(token);
    }
    static getUserByToken(token) {
        if (this.session.has(token)) {
            let userId = this.session.get(token);
            let user = null;
            this.userMap.forEach((value, key) => {
                if (value.userId == userId) {
                    user = value;
                    return user;
                }
            });
            return user;
        }
    }
}
exports.GmUserModel = GmUserModel;
GmUserModel.userMap = new Map()
    .set("jhao", new GmUserModel(0, "jhao", "666666")); //
GmUserModel.session = new Map();
